<template>
	<view class="follow-container">
		<!-- 头部选项TAB -->
		<view class="follow-tab">
			<view class="follow-tab-content">
				<view class="follow-tab-item" :class="{select:currentIndex===0}" @click="currentIndex=0">
					文章
				</view>
				<view class="follow-tab-item" :class="{select:currentIndex===1}" @click="currentIndex=1">
					作者
				</view>
			</view>
		</view>
		<!-- 内容 -->
		<view class="follow-list-container">
			<swiper class="follow-list-swiper" :current="currentIndex" @change="currentIndex=$event.detail.current">
				<swiper-item>
					<ListItem v-if="articleList.length" :ArticleList="articleList" :showloading="false"></ListItem>
					<view v-else class="follow-list-noData">
						暂无收藏文章
					</view>
				</swiper-item>
				<swiper-item>
					<AuthorList v-if="authorList.length" :authorList="authorList"
						@fllowAuthorLongpress='fllowAuthorLongpress'></AuthorList>
					<view v-else class="follow-list-noData">
						暂无关注作者
					</view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
	export default {
		onLoad() {
			// #ifdef MP-WEIXIN
			//判断是否登录
			if (this.$Route.meta.checking && !this.userInfo) {
				uni.redirectTo({
					url: '/pages/userInfo/login/login'
				})
				return
			}
			// #endif
			uni.$on('updateFolloArticleList', () => {
				this.getFollowList('noLoding')
			})
			uni.$on('updataFollowAuthor', () => {
				this.getAuthorList('noLoding')
			})
			//获取收藏的文章列表
			this.getFollowList()
			//获取关注作者名单
			this.getAuthorList()
		},
		data() {
			return {
				currentIndex: 0,
				articleList: [],
				authorList: []
			}
		},
		methods: {
			//获取文章列表
			async getFollowList(isLoading) {
				const followList = await this.$http.get_follow_list({
					userId: this.userInfo._id,
					isLoading
				})
				this.articleList = followList
			},
			//获取关注作者列表
			async getAuthorList(isLoading) {
				const authorList = await this.$http.get_author_list({
					userId: this.userInfo._id,
					isLoading
				})
				this.authorList = authorList
			},
			//长按作者取消关注
			async fllowAuthorLongpress(id) {
				const {
					msg
				} = await this.$http.updata_follow_author({
					authorId: id,
					userId: this.userInfo._id
				})
				uni.showToast({
					title: msg,
					icon: "none"
				})
				//改变用户存储数据
				let followIds = [...this.userInfo.author_likes_ids]
				if (followIds.includes(id)) {
					//取消关注
					followIds = followIds.filter(item => item !== id)
				} else {
					//关注
					followIds.push(id)
				}
				//更改储存数据
				this.updetaUserInfo({
					...this.userInfo,
					author_likes_ids: followIds
				})
				this.getAuthorList()
			}
		}
	}
</script>
<style>
	page {
		height: 100%;
		display: flex;
	}
</style>

<style lang="scss" scoped>
	@import 'css/follow.scss'
</style>
